package com.bnq.control.common.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatEditText;

import com.bnq.control.R;


/**
 * 具有清除文本内容功能的editText
 *
 * @author zou
 */
public class EditTextView extends AppCompatEditText {

    private final static String TAG = "EditTextView";

    public final static int DRAWABLE_LEVEL_DELETE = 0;
    public final static int DRAWABLE_LEVEL_RIGHT = 1;
    public final static int DRAWABLE_LEVEL_ERROR = 2;
    public final static int DRAWABLE_LEVEL_NULL = 3;
    private boolean isFocus = false;

    Context mContext;
    private Drawable img;       // 居右的图标
    private final Rect mTemp = new Rect();
    private OnEditTextViewListener mOnEditTextViewListener;

    public EditTextView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public EditTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public EditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    /**
     * 加载资源
     */
    private void init() {
        img = mContext.getResources().getDrawable(
                R.drawable.account_register_delete_img);
        setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        // 监听输入事件
        addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isFocus) {
                    // 设置图片
                    setDrawable(DRAWABLE_LEVEL_DELETE);
                    if (mOnEditTextViewListener != null) {
                        mOnEditTextViewListener.onTextChange(EditTextView.this,s.toString());
                    }
                }

            }
        });
    }

    /**
     * 设置删除图片
     */
    public void setDrawable(int drawableLevel) {
        if (drawableLevel == DRAWABLE_LEVEL_DELETE) {
            img.setLevel(DRAWABLE_LEVEL_DELETE);
            if (length() < 1 || "".equals(getText().toString())) {
                // 隐藏右边的删除图片
                setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            } else {
                // 设置右边的删除图片
                setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
            }
        } else if (drawableLevel == DRAWABLE_LEVEL_RIGHT) {
            img.setLevel(DRAWABLE_LEVEL_RIGHT);
            setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
        } else if (drawableLevel == DRAWABLE_LEVEL_ERROR) {
            img.setLevel(DRAWABLE_LEVEL_ERROR);
            setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
        } else if (drawableLevel == DRAWABLE_LEVEL_NULL) {
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    /**
     * 处理删除事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (img.getLevel() == DRAWABLE_LEVEL_DELETE
                && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getX();
            int eventY = (int) event.getY();

            mTemp.set(0, 0, getWidth(), getHeight() * 2);
            mTemp.left = mTemp.right - img.getIntrinsicWidth() * 2;
            mTemp.top = -img.getIntrinsicHeight();
            mTemp.right = getWidth() + img.getIntrinsicWidth();
            // 获取手指坐标与删除图片的坐标
            if (mTemp.contains(eventX, eventY) && this.isFocus) {
                setText("");
                if (null != mOnEditTextViewListener) mOnEditTextViewListener.clearInputText();
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        isFocus = focused;
        if (focused) {
            if (img.getLevel() == DRAWABLE_LEVEL_DELETE) {
                if (length() > 0) {
                    setCompoundDrawablesWithIntrinsicBounds(null, null, img,
                            null);
                }
            } else {
                img.setLevel(DRAWABLE_LEVEL_DELETE);
                if (length() > 0) {
                    setCompoundDrawablesWithIntrinsicBounds(null, null, img,
                            null);
                }
            }
        } else {
            if (img.getLevel() == DRAWABLE_LEVEL_DELETE) {
                setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }
        }
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    public void setOnEditTextViewListener(OnEditTextViewListener editTextViewListener) {
        this.mOnEditTextViewListener = editTextViewListener;
    }

    public interface OnEditTextViewListener {
        void clearInputText();      // 清除输入的文字

        default void onTextChange(EditTextView textView, String str){

        }
    }
}